<?php

/**
 * Created by Sublime.
 * User: yinni
 * Date: 2017/10/07
 * Time: 16:41
 */
namespace App\Http\Controllers\Admin\Monitordefine;

use Log;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\Admin\MonitordefineService;
use App\Http\Requests\Admin\UserRequest;
use \App\Http\Controllers\Admin\BaseController;
use DB;
/**
 * 报警响应设置
 * Class MonitordefineController
 * @package App\Http\Controllers\Admin\Monitordefine
 */
class MonitordefineController extends BaseController
{
    protected $service;

    public function __construct(MonitordefineService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $result = $this->service->index();
        return request()->ajax() ? $result : view('themes.admin.monitordefine.index')->with($result);
    }

    public function alltimeinfo() {
        $alltime_data = $this->service->ajaxData();
        // $json_alltime_data = responce()->json($alltime_data);
        // dump($alltime_data);die;
        // Log::info('错误提示信息');
        // die();
        return $alltime_data;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $monitor_id
     * @return \Illuminate\Http\Response
     */
    public function edit($monitor_id)
    {
        // dump($monitor_id);die;
        $result = $this->service->edit($monitor_id);
        return view('themes.admin.monitordefine.edit')->with($result);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $ids
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $route = $this->service->update($request->all(), $id);
        return redirect()->route($route);
    }

    // ajax新增测点请求
    public function addmonitor(Request $request) {
        // 传输过来的Aajx对象信息
        $monitor_info = $request->input('monitorInfo');
        // $monitor_info = json_encode($monitor_info);
        // $monitor_info = json_encode($monitor_info);

        $data['monitor_id'] = $monitor_info['monitor_id'];
        $data['loop_name'] = $monitor_info['loop_name'];
        if(!empty($data['monitor_id']) && !empty($data['loop_name'])) {
            $data['cable_temp'] = $monitor_info['cable_temp'];
            $data['smoke'] = $monitor_info['smoke'];
            $data['fire'] = $monitor_info['fire'];
            $data['near_infrared'] = $monitor_info['near_infrared'];
            // $data['status'] = $monitor_info['status'];
            $data['location'] = $monitor_info['location'];
            $res = DB::table('monitor')->insert($data);  
            // dump($res);die;   
            if ($res) {
                return response()->json(array(
                    'res' => 1
                ));
            } else {
                return response()->json(array(
                    'res' => 0
                ));
            }      
        } else{
            return response()->json(array(
                    'res' => 0
                ));
        }

    }


}